package com.xiaolun.Utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author 硕鼠
 * @Date 2020/4/16 17:38
 * @Version 1.0
 * @Description 文件说明
 */
public class JsonUtils {

    //方法1
    public static String getJson(Object object){
        return getJson(object,"yyyy-MM-dd HH:mm:ss");
    }

    //方法2
    public static String getJson(Object object,String dataFormat){
        ObjectMapper mapper = new ObjectMapper();
        //关闭时间戳功能
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        //自定义格式对象
        SimpleDateFormat sdf = new SimpleDateFormat(dataFormat);
        //让mapper指定时间日期
        mapper.setDateFormat(sdf);
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
